const express = require("express");
const Router = express.Router();

const {
    isEntryParame,
    noparames
} = require("../../utils/validate/validate");
const {
    createTime
} = require("../../utils/time/time");

const configSql = require("../../db/db");
// 获取所有的旅游景点
Router.get("/all_travel", (req, res) => {
    let {
        title,
        city,
        pageSize,
        page
    } = req.query
    pageSize = pageSize ? pageSize : 10
    page = page ? page - 1 : 0
    let data = {
        title,
        city
    }
    let arr = []
    for (let key in data) {
        if (data[key]) {
            arr.push({
                value: data[key],
                key: key
            })
        }
    }
    let sql = `select * from travel_list limit ${page * pageSize},${pageSize}`
    let s = `select count(*) from travel_list`
    configSql(s).then(count => {
        configSql(sql).then(result => {
            res.send({
                code: 0,
                msg: "获取成功",
                data: result,
                count: count[0]['count(*)'],
            })
        })
    })
})

// 修改旅游景点
Router.post("/update_travel", (req, res) => {
    let {
        id,
        title,
        image,
        introduce,
        price,
        city,
        type,
        address
    } = req.body
    let obj = {
        id,
        title,
        image,
        introduce,
        price,
        city,
        type,
        address
    }
    // 参数缺失验证
    isEntryParame(obj, (d) => {
        if (!d.boo) {
            res.status(500).send({
                code: 500,
                msg: d.msg,
            });
            return;
        }
    });
    let sql = `update travel_list set title='${title}', image='${image}',introduce='${introduce}',price='${price}',city='${city}',type='${type}',address='${address}'where id='${id}'`
    configSql(sql).then(result => {
        if (result.message) {
            res.status(200).send({
                code: 0,
                msg: "修改成功"
            })
        }
    })
})

// 查询旅游id
Router.get("/get_travel", (req, res) => {
    let {
        id
    } = req.query
    if (!id) {
        res.send({
            code: 500,
            msg: "参数缺失"
        })
    } else {
        let sql = `select * from travel_list where id='${id}'`
        configSql(sql).then(result => {
            res.send({
                code: 0,
                msg: "获取成功",
                result: result[0]
            })
        })
    }
})
// 删除旅游景点
Router.post("/del_travel", (req, res) => {
    let {
        id
    } = req.body
    if (!id) {
        res.send({
            code: 500,
            msg: "参数缺失"
        })
    } else {
        let sql = `DELETE FROM travel_list WHERE id ='${id}'`
        configSql(sql).then(result => {
            res.send({
                code: 0,
                msg: "删除成功",
            })
        })
    }
})
// 获取单个模块的景点
Router.get('/getType/travel', (req, res) => {
    let {
        type,
        count
    } = req.query
    count = count || 5
    if (!type || !count) {
        res.send({
            code: 500,
            msg: '参数缺失',
        })
    } else {
        let sql = `select * from travel_list where type='${type}'`
        configSql(sql).then(result => {
            res.send({
                code: 0,
                msg: "获取成功",
                data: result.length > 5 ? result.slice(0, count) : []
            })

        })
    }
})

// 获取城市列表
Router.get("/getTravel_title", (req, res) => {

    let city_list = [{
        city: '全部',
        code: 0,
    }, {
        city: "秦安",
        code: 1201
    }, {
        city: '湖州',
        code: 1202
    }, {
        city: '济宁',
        code: 1203
    }, {
        city: '南平',
        code: 1204
    }, {
        city: '池州',
        code: 1205
    }, {
        city: '温州',
        code: 1206
    }, {
        city: '福州',
        code: 1207
    }, {
        city: '丽水',
        code: 1208
    }]
    res.send({
        code: 0,
        msg: "获取成功",
        data: city_list
    })

})

// 根据城市获取景点
Router.get("/getTeavel_contxt", (req, res) => {
    let {
        code,
        address
    } = req.query
    if (!address) {
        res.send(noparames)
        return
    }
    let sql
    if (code) {
        sql = `select * from travel_list where sub_item='${code}'and address='${address}'`
    } else {
        sql = `select * from travel_list where address='${address}' LIMIT 0,5`
    }

    configSql(sql).then(result => {
        res.send({
            code: 0,
            msg: '获取成功',
            data: result

        })


    })
})

// 获取旅游路线
Router.get("/getTavel_line", (req, res) => {
    let {
        type
    } = req.query
    if (!type) {
        res.send(noparames)
    }
    let sql = `select * from travel_list where type='${type}'`
    configSql(sql).then(result => {
        res.send({
            code: 0,
            msg: '获取成功',
            data: result
        })

    })
})


// 首页分类标题
Router.get("/them_list", (req, res) => {
    let {
        parent_id
    } = req.query
    if (!parent_id) {
        res.send(noparames)
        return
    }

    let sql = `select child.name,child.id,child.parent_id from child inner join parent on child.parent_id=${parent_id} ORDER BY child.id ASC`
    configSql(sql).then(result => {
        let times = null
        // 处理数据
        let newArr = []
        for (let val of result) {
            if (!newArr.some(item => item.id == val.id)) {
                newArr.push(val)
            }
        }
        res.send({
            code: 0,
            msg: '获取成功',
            data: newArr
        })
        // times = setTimeout(() => {
        //     res.send({
        //         code: 0,
        //         msg: '获取成功',
        //         data: newArr
        //     })
        // }, 200)

    })
})

// 首页分类分级
Router.get("/theme_recommend", (req, res) => {
    let {
        sub_item
    } = req.query
    if (!sub_item) {
        res.send(noparames)
    }
    let sql = `select * from travel_list where sub_item='${sub_item}'`
    configSql(sql).then(result => {
        res.send({
            code: 0,
            msg: '获取成功',
            data: result
        })
    })
})



// 顶部搜索
Router.post('/details_list', (req, res) => {
    let {
        type,
        keyword
    } = req.body
    if (!type || !keyword) {
        res.send(noparames)
    }
    let store = ['travel_list', 'hotel_list', 'strategy_list']
    let sql = `select * from ${store[type]} where title like '%${keyword}%'`
    configSql(sql).then(result => {
        res.send({
            code: 0,
            msg: '获取成功',
            data: result
        })
    })
})




// 订单号
Router.post("/order_play", (req, res) => {
    let {
        username,
        price,
        title
    } = req.body
    if (!username ||
        !price ||
        !title) {
        res.send(noparames)
    }
    let lette = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
    let time = +new Date()
    let order_name = 0
    for (let i = 0; i < 4; i++) {
        order_name += '' + lette[Math.floor(Math.random() * lette.length)]
    }
    order_name = order_name + time

    let sql = `INSERT INTO order_list(username,order_num,price,create_time,title)VALUES('${username}','${order_name}','${price}','${createTime()}','${title}')`
    configSql(sql).then(result => {
        res.send({
            code: 0,
            msg: '支付成功',
        })
    })
})


// 获取订单列表
Router.post('/get_order_list', (req, res) => {
    let {
        uid
    } = req.body
    if (!uid) {
        res.send(noparames)
    }
    let sql = `select * from order_list where uid=${uid}`
    configSql(sql).then(result => {
        res.send({
            code: 0,
            msg: '获取成功',
            data: result
        })
    })
})






// 导出路由
module.exports = Router;